package utils.animate
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	
	import utils.EnterFrameTimer;

	public class TweenProperty extends EventDispatcher
	{
		private var timer:EnterFrameTimer;
		private var property:String;
		private var _target:*;
		private var tv:Number;
		private var fv:Number;
		private var _duration:Number;
		private var speed:Number;
		private var increment:int;
		
		public function TweenProperty()
		{
			initTimer();
		}
		
		public function get target():*
		{
			return _target;
		}
		
		private function initTimer():void
		{
			timer = new EnterFrameTimer(20);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		
		private function onTimer(event:TimerEvent):void
		{
			increment++;
			_target[property] += speed ;
			if (increment > _duration)
			{
				stop();
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		public function set duration(v:Number):void
		{
			_duration = v * 50;
			
		}
		
		public function setTarget(target:*,property:String,fv:Number,tv:Number):void
		{
			this.property = property;
			_target = target;
			_target[property] = fv;
			this.fv = fv;
			this.tv = tv;
			speed = (tv - fv) / _duration;
			increment = 0;
		}
		
		public function start():void
		{
			timer.start();
		}
		
		public function stop():void
		{
			timer.reset();
		}
		
		public function destroy():void
		{
			stop();
			timer.removeEventListener(TimerEvent.TIMER,onTimer);
			timer = null;
			_target = null;
		}
	}
}